package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.LeaseActualAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.LeaseRealHandleDeviceInfo;
import com.isunland.managesystem.entity.LeaseRealHandleDeviceInfoListOriginal;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017-02-16 13:48:00
 */

public class LeaseActualListFragment extends BaseListFragment {

  static final String EXTRA_MAINID =
      LeaseActualListFragment.class.getSimpleName() + ".EXTRA_MAINID";
  private ArrayList<LeaseRealHandleDeviceInfo> mList;
  private LeaseActualAdapter<LeaseRealHandleDeviceInfo> mAdapter;
  private String mainId;

  public static LeaseActualListFragment newInstance(String mainId) {
    LeaseActualListFragment fragment = new LeaseActualListFragment();
    Bundle args = new Bundle();
    args.putString(EXTRA_MAINID, mainId);
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    BaseVolleyActivity mActivity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar = mActivity.getSupportActionBar();

    if (actionBar != null) {
      actionBar.setTitle(R.string.actualHandleDetail);
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
    mList = new ArrayList<>();
    mainId = getArguments().getString(EXTRA_MAINID);
    mAdapter = new LeaseActualAdapter<>(mActivity, mList);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    int padding = MyUtils.dip2px(getActivity().getApplicationContext(), 16);
    //noinspection deprecation
    mListview.setDivider(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
    mListview.setDividerHeight(padding);
    mListview.setPadding(0, 0, 0, padding);

    setListAdapter(mAdapter);
  }

  @Override public String getUrl() {
    return ApiConst.URL_LEASE_ACTUAL_LIST;
    //return ApiConst.URL_LATEST_VERSION;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();

    params.put("mainId", mainId);

    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    //json = MyUtils.readAssetsFile(mActivity, "testAppointmentOrder.json");
    LeaseRealHandleDeviceInfoListOriginal result =
        new Gson().fromJson(json, LeaseRealHandleDeviceInfoListOriginal.class);

    if (result.getResult() == Base.SUCCESS && result.getRows() != null) {
      //if (result.getRows() != null) {
      //获取列表成功
      //LogUtil.i("date="+result.getRows().get(0).getRegDate());
      mList.clear();
      mList.addAll(result.getRows());
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;

    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    LeaseRealHandleDeviceInfo item = mAdapter.getItem(position - mListview.getHeaderViewsCount());
    LeaseActualDetailActivity.newInstance(this, mActivity, item, 0);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //inflater.inflate(R.menu.menu_add, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        break;
      //case R.id.menu_item_add:
      //    //startActivityForResult(new Intent(getActivity(),AddAppointmentOrderActivity.class),REQUEST_ADD);
      //    AddAppointmentOrderActivity.startActivityForResult(this, mActivity, null, REQUEST_ADD);
      //    break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  //@Override
  //public void click(View v) {
  //    if (v == null) return;
  //    switch (v.getId()) {
  //        case R.id.tv_cancel://取消
  //            if (v.getTag() instanceof Integer) {
  //                int position = (Integer) v.getTag();
  //                LeaseMainList item = mAdapter.getItem(position);
  //                ToastUtil.show("取消" + item.getId());
  //            }
  //            return;
  //        case R.id.tv_reminder://催单
  //            if (v.getTag() instanceof Integer) {
  //                int position = (Integer) v.getTag();
  //                LeaseMainList item = mAdapter.getItem(position);
  //                ToastUtil.show("催单" + item.getId());
  //            }
  //            return;
  //        default:
  //            break;
  //    }
  //}
}
